package 栈算法;

import org.junit.Test;

import java.util.HashMap;
import java.util.Stack;

public class isValid20 {
    /**
     * 是否有效 利用的栈的对称性的
     *
     * @param s
     * @return
     */
    public boolean isValid(String s) {
        HashMap<Character, Character> map = new HashMap<>();
        map.put('}', '{');
        map.put(')', '(');
        map.put(']', '[');

        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '{' || s.charAt(i) == '[' || s.charAt(i) == '(') {
                stack.add(s.charAt(i));
            } else {
                if (!stack.isEmpty() && stack.peek().equals(map.get(s.charAt(i)))) {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    @Test
    public void test() {
        boolean valid = isValid("([)]");
        System.out.println(valid);

    }
}
